package com.atguigu.interview.study.lru;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gcq
 * @Create 2020-12-13
 */
public class LRUCacheDemo<k,v> extends LinkedHashMap<k,v> {

    private int capacity; // 缓存坑位

    /*
    true 访问顺序
    false 插入顺序
     */
    public LRUCacheDemo(int capacity) {
        super(capacity, 0.75F,false);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<k, v> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {

        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);

        lruCacheDemo.put(1,"a");
        lruCacheDemo.put(2,"b");
        lruCacheDemo.put(3,"c");
        System.out.println(lruCacheDemo.keySet());


        lruCacheDemo.put(4,"c");
        System.out.println(lruCacheDemo.keySet());


        lruCacheDemo.put(3,"c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3,"c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3,"c");
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(5,"x");
        System.out.println(lruCacheDemo.keySet());
    }
}
/**
 [1, 2, 3]
 [2, 3, 4]

 [2, 4, 3]
 [2, 4, 3]
 [2, 4, 3]

 [4, 3, 5]
 */
/**
 [1, 2, 3]
 [2, 3, 4]

 [2, 3, 4]
 [2, 3, 4]
 [2, 3, 4]

 [3, 4, 5]
 */
